import { arrayMethods } from "./array.js"
import { def } from "./def.js"
import { defineReactive } from "./defineReactive.js"
import observe from "./observe.js"

export default class Observer {
    constructor(val) {
        def(val, '__ob__', this, false)
        if (Array.isArray(val)) {
            Object.setPrototypeOf(val, arrayMethods)
            // 给数组添加observer
            this.observeArray(val)
        } else {
            this.walk(val)
        }
    }
    walk = (value) => {
        for (let key in value) {
            defineReactive(value, key)
        }
    }
    observeArray = (array) => {
        for (let i = 0, l = array.length; i < l; i++) {
            observe(array[i])
        }
    }
}